home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / IrComm Remote / DVDRemote / Hack / zzOther / Hack68k / SillyBalls.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-16  |  5.8 KB  |  214 lines  |  [TEXT/CWIE]

  1. /*------------------------------------------------------------------------------
  2. #
  3. #    Macintosh Developer Technical Support
  4. #
  5. #    Simple Color QuickDraw Sample Application
  6. #
  7. #    SillyBalls
  8. #
  9. #    SillyBalls.c    -    C Source
  10. #
  11. #    Copyright © 1988 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    1.0                    8/88
  15. #
  16. #    Components:    SillyBalls.c        August 1, 1988
  17. #                SillyBalls.make        August 1, 1988
  18. #
  19. #    This is a very simple sample program that demonstrates how to use Color 
  20. #    QuickDraw.  It is about two pages of code, and does nothing more than open
  21. #    a color window and draw randomly colored ovals in the window.
  22. #    
  23. #    The purpose is to show how to get some initial results with Color QuickDraw.
  24. #    It is a complete program and is very short to be as clear as possible.
  25. #    
  26. #    It does not have an Event Loop.  It is not fully functional in the sense that
  27. #    it does not do all the things you would expect a well behaved Macintosh 
  28. #    program to do, like size the window naturally, have an event loop, use menus, 
  29. #    etc.
  30. #
  31. #    See Sample and TESample for the general structure and MultiFinder techniques that
  32. #    we recommend that you use when building a new application.
  33. #
  34. ------------------------------------------------------------------------------*/
  35.  
  36.     
  37. //    Version 1.0:    6/2/88
  38. //                    7/20/88     DJB    Converted to C
  39. //    
  40. //    purpose        To demonstrate a simple color App using Color QuickDraw.
  41. //                        It draws colored balls in a color window, then uses colored
  42. //                        text inverted in the ball.  The ball location and color is Random.
  43. //                        
  44. //                        This program was written by Bo3b Johnson, 1/88.
  45. //                        
  46. //                        The inverted Bob text was a Skippy Blair special concept,
  47. //                        kept for obvious aesthetic reasons.
  48.  
  49. //MW -cut out some other program descriptions.-
  50.  
  51. //MW ** Metrowerks note **
  52. //   All changed code by Metrowerks is commented by "//MW".
  53. //   There is one type of modification to the original source:
  54. //   • Added argument type and return type to function definitions.
  55. //       In order to pass with extended error checking on.
  56. //    
  57. //   8/31/93 JA
  58.  
  59.  
  60. #include <Types.h>
  61. #include <Memory.h>
  62. #include <Quickdraw.h>
  63. #include <Fonts.h>
  64. #include <Events.h>
  65. #include <Menus.h>
  66. #include <Windows.h>
  67. #include <TextEdit.h>
  68. #include <Dialogs.h>
  69. #include <OSUtils.h>
  70. #include <ToolUtils.h>
  71. #include <SegLoad.h>
  72. #include <Sound.h>
  73.  
  74. /* Constants */
  75. #define BallWidth        20
  76. #define BallHeight        20
  77. #define BobSize            8        /* Size of text in each ball */
  78.  
  79. /* Globals */
  80. Rect    windRect;
  81.     
  82. /* Prototypes */
  83. void Initialize(void);
  84. void NewBall(void);
  85.  
  86. // 
  87. //    Main body of program SillyBalls
  88. //
  89.  
  90. //MW specified argument and return type.
  91. int main(void)
  92. {
  93.     Initialize();
  94.     
  95.     do {
  96.         NewBall();
  97.     } while (!Button());
  98.  
  99.     return 0;    
  100. }
  101.  
  102. // 
  103. //    Initialize everything for the program, make sure we can run
  104. //
  105.  
  106. //MW specified argument and return type.
  107. void Initialize(void)
  108. {
  109.     WindowPtr    mainPtr;
  110.     OSErr        error;
  111.     SysEnvRec    theWorld;
  112.         
  113.     //
  114.     //    Test the computer to be sure we can do color.  
  115.     //    If not we would crash, which would be bad.  
  116.     //    If we can’t run, just beep and exit.
  117.     //
  118.  
  119.     error = SysEnvirons(1, &theWorld);
  120.     if (theWorld.hasColorQD == false) {
  121.         SysBeep(50);
  122.         ExitToShell();                    /* If no color QD, we must leave. */
  123.     }
  124.     
  125.     /* Initialize all the needed managers. */
  126.     InitGraf(&qd.thePort);
  127.     InitFonts();
  128.     InitWindows();
  129.     InitMenus();
  130.     TEInit();
  131.     InitDialogs(nil);
  132.     InitCursor();
  133.  
  134.     //
  135.     //    To make the Random sequences truly random, we need to make the seed start
  136.     //    at a different number.  An easy way to do this is to put the current time
  137.     //    and date into the seed.  Since it is always incrementing the starting seed
  138.     //    will always be different.  Don’t for each call of Random, or the sequence
  139.     //    will no longer be random.  Only needed once, here in the init.
  140.     //
  141.     GetDateTime((unsigned long*) &qd.randSeed);
  142.  
  143.     //
  144.     //    Make a new window for drawing in, and it must be a color window.  
  145.     //    The window is full screen size, made smaller to make it more visible.
  146.     //
  147.     windRect = qd.screenBits.bounds;
  148.     InsetRect(&windRect, 50, 50);
  149.     mainPtr = NewCWindow(nil, &windRect, "\pBob Land", true, documentProc, 
  150.                         (WindowPtr) -1, false, 0);
  151.         
  152.     SetPort(mainPtr);                        /* set window to current graf port */
  153.     TextSize(BobSize);                        /* smaller font for drawing. */
  154. }
  155.  
  156.  
  157. // 
  158. //    NewBall: make another ball in the window at a random location and color.
  159. //
  160.  
  161. //MW -specified argument and return type.-
  162. void NewBall(void)
  163. {
  164.     RGBColor    ballColor;
  165.     Rect        ballRect;
  166.     long int    newLeft,
  167.                 newTop;
  168.     
  169.     // 
  170.     //    Make a random new color for the ball.
  171.     //
  172.     ballColor.red   = (unsigned short)Random();
  173.     ballColor.green = (unsigned short)Random();
  174.     ballColor.blue  = (unsigned short)Random();
  175.     
  176.     // 
  177.     //    Set that color as the new color to use in drawing.
  178.     //
  179.     RGBForeColor (&ballColor);
  180.  
  181.     //    
  182.     //    Make a Random new location for the ball, that is normalized to the window size.  
  183.     //    This makes the Integer from Random into a number that is 0..windRect.bottom
  184.     //    and 0..windRect.right.  They are normalized so that we don't spend most of our
  185.     //    time drawing in places outside of the window.
  186.     //
  187.     newTop = Random();    newLeft = Random();
  188.     newTop = ((newTop+32767) * windRect.bottom)/65536;
  189.     newLeft = ((newLeft+32767) * windRect.right)/65536;
  190.     SetRect(&ballRect, (short)newLeft, (short)newTop, (short)(newLeft+BallWidth), (short)(newTop+BallHeight));
  191.     
  192.     //
  193.     //    Move pen to the new location, and paint the colored ball.
  194.     //
  195.     MoveTo((short)newLeft, (short)newTop);
  196.     PaintOval (&ballRect);
  197.     
  198.     //
  199.     //    Move the pen to the middle of the new ball position, for the text
  200.     //
  201.     MoveTo((short)(ballRect.left + BallWidth/2 - BobSize), 
  202.         (short)(ballRect.top + BallHeight/2 + BobSize/2 -1));
  203.     
  204.     //    
  205.     //    Invert the color and draw the text there.  This won’t look quite right in 1 bit
  206.     //    mode, since the foreground and background colors will be the same.
  207.     //    Color QuickDraw special cases this to not invert the color, to avoid
  208.     //    invisible drawing.
  209.     //
  210.     InvertColor(&ballColor); 
  211.     RGBForeColor(&ballColor);
  212.     DrawString("\pBob");
  213. }
  214.